home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3106 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.4 KB

  1. Path: news.primenet.com!not-for-mail
  2. From: gbe@primenet.com (Gary Edstrom)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP!!! - loop control problems
  5. Date: 20 Jan 1996 19:57:01 -0700
  6. Organization: Sequoia Software
  7. Sender: root@primenet.com
  8. Message-ID: <3101abf0.164809733@news.primenet.com>
  9. References: <4ds174$6t0@bolivia.it.earthlink.net>
  10. X-Posted-By: ip008.lax.primenet.com
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. macc@earthlink.net (OB1) wrote:
  14.  
  15. >I have the following code in a program I'm writing:
  16. >
  17. >const long SCREEN_SIZE = 320 * 200;
  18. >long count = 0;
  19. >while (count <= SCREEN_SIZE)
  20. >{
  21. >    ... code (count gets incremented)
  22. >}
  23. >
  24. >When I trace through my program, I find that the loop end condition is
  25. >always true and the code inside the loop is never run. I looking for
  26. >any ideas on why this doesn't work.
  27.  
  28. If you are using a 16 bit compiler, the product of 320 * 200 is 64,000
  29. which is interpreted as a negative number in the world of 16 bit
  30. integers.  It remains negative when it is sign extended to a 32 bit
  31. number.  As a result, count is never less than screen size.
  32.  
  33. Try this:
  34.  
  35. const long SCREEN_SIZE = (long) 320 * 200;
  36.  
  37. I believe you'll find that it works.
  38.  
  39. Gary
  40.  
  41. --
  42. Gary Edstrom <gbe@primenet.com> | Sequoia Software
  43. PO Box 9573                     | Programming & Technical Services
  44. Glendale CA 91226-0573          | PGP Key ID: 0x1A0D44BD
  45. PGP Fingerprint: 72 AA 4F 73 05 53 89 C6  8A EE F4 EE D1 C0 13 8D 
  46.